package com.zhong.gang.app.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.databinding.LayoutWaidiaoCarBinding;
import com.zhong.gang.app.ui.address.AddressAct;
import com.zhong.gang.app.ui.address.AddressBean;
import com.zhong.gang.app.ui.carorder.CarOrderAct;
import com.zhong.gang.app.ui.carorder.OrderListener;
import com.zhong.gang.app.ui.carorder.OrderNumBean;

public class WaiDiaoCarInputView extends FrameLayout
        implements RadioGroup.OnCheckedChangeListener,AddressListener, OrderListener {
    private LayoutWaidiaoCarBinding binding;
    private AddressBean addressBean;
    private OrderNumBean orderNumBean;
    private CarViewListener listener;

    public void setListener(CarViewListener listener) {
        this.listener = listener;
    }

    public WaiDiaoCarInputView(@NonNull Context context) {
        super(context);
        initView();
    }

    public WaiDiaoCarInputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WaiDiaoCarInputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_waidiao_car,null);
        binding = DataBindingUtil.bind(view);
        binding.setView(this);
        binding.radio.setOnCheckedChangeListener(this);
        ((RadioButton)binding.radio.getChildAt(0)).setChecked(true);
        onCheckedChanged(binding.radio,0);
        setViewVisibility(View.GONE);
        this.addView(view);
    }

    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.add_gs_car:
                CarOrderAct.openAct(getContext(),this);
                break;
            case R.id.address_layout:
                AddressAct.openAct(getContext(),Api.gangChangeList,
                        this);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int n = 0 ; n < group.getChildCount();n++){
            View view = group.getChildAt(n);
            if (view instanceof RadioButton){
                if (((RadioButton) view).isChecked()){
                    setStatus(n==1);
                    binding.addressLayout.setEnabled(n==1);
                    setViewVisibility(n==1?View.VISIBLE:View.GONE);
                }
            }
        }
    }
    
    private void setViewVisibility(int visibility){
        binding.addressLayout.setVisibility(visibility);
        binding.carNumberLayout.setVisibility(visibility);
        binding.carNumberView.setVisibility(visibility);
        binding.addressTypeView.setVisibility(visibility);
    }

    /**
     * 设置状态
     * @param status
     */
    private void setStatus(boolean status){
        binding.addGsCar.setVisibility(status?GONE:VISIBLE);
        binding.carNumEdt.setEnabled(status);
        binding.addressLayout.setEnabled(status);
        binding.carNumEdt.setText("");
        binding.addressTv.setText("");
        orderNumBean = null;
        addressBean = null;
        if (listener != null)
            listener.onAddress((AddressBean)null);
    }

    @Override
    public void onAddressBack(AddressBean addressBean) {
        this.addressBean = addressBean;
        binding.addressTv.setText(addressBean.getName());
        if (listener != null)
            listener.onAddress(addressBean);
        setViewVisibility(View.VISIBLE);
    }

    @Override
    public void onOrderListener(OrderNumBean bean) {
        this.orderNumBean = bean;
        binding.carNumEdt.setText(bean.getCarNum());
        binding.addressTv.setText(bean.getEndLocal());
        if (listener != null)
            listener.onAddress(bean);
        setViewVisibility(View.VISIBLE);
    }

    public String getCarNum(){
        return binding.carNumEdt.getText().toString();
    }
    /**
     * 获取派车单id
     * @return
     */
    public int getOrderNumberId() {
        if (orderNumBean == null)
            return -1;
        return orderNumBean.getId();
    }

    /**
     * 获取地址id
     * @return
     */
    public int getAddressId(){
        if (addressBean != null)
            return addressBean.getId();
        if (orderNumBean != null)
            return orderNumBean.getEndLocalId();
        return -1;
    }




    public LayoutWaidiaoCarBinding getBinding() {
        return binding;
    }

    /**
     * 获取选中类型
     * @return
     */
    public int getCarType(){
        for (int n = 0 ; n < binding.radio.getChildCount();n++){
            View view = binding.radio.getChildAt(n);
            if (view instanceof RadioButton){
                if (((RadioButton) view).isChecked()){
                   return n;
                }
            }
        }
        return 0;
    }

    public interface CarViewListener{
        void onAddress(AddressBean bean);
        void onAddress(OrderNumBean bean);
    }
}
